<template>
    <div class="saveUpdate">
        <el-dialog :close-on-click-modal="false" :title="'追分'" :visible.sync="showDialog" @close="$emit('update:showAddScore', false)"
                   :showAddScore="showAddScore" width="500px">
            <el-form :model="AddScoreInfo" ref="saveChn" label-width="90px" size="mini">
                <dialog-wrapper :MaxWidth="400">
                    <el-form-item prop="remark" label="订单金额:">
                        <span>{{this.rowInfo.amount}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="充值金额:">
                        <span>{{this.rowInfo.real_amount}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="支付方式:">
                        <span>{{this.rowInfo.pay_type | FormartToText}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="收款账号">
                        <span>{{this.receipt_addresses}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="到账金额:">
                        <span>{{this.rowInfo.recharge_amount}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="追分金额:">
                        <span>{{this.rowInfo.amount}}</span>
                    </el-form-item>
                    <el-form-item prop="remark" label="备注:"   :rules="[{ required: true, message: '备注不能为空'}]">
                        <el-input type="textarea" :rows="5" v-model="AddScoreInfo.remark"
                                  placeholder="请输入备注..."></el-input>
                    </el-form-item>
                </dialog-wrapper>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="$emit('update:showAddScore', false)" size="small">取 消</el-button>
                <!-- <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button> -->
                <el-button type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import DialogWrapper from '@/components/DialogWrapper'
    import {sendRechargeOrderschase, getOrderInfoByID, getReceiptAddresses} from '@/api'
    import {FormartPayType, FormarOrderMethodType} from "../../utils/CommonJs"
    import {useVcode} from "../../utils/CommonJs";

    export default {
        components: {DialogWrapper},
        data() {
            return {
                setbank: false,
                showDialog: this.showAddScore,
                loading: false,
                AddScoreInfo: {remark: ''},
                DetailInfo: {},
                payInfo: {},
                receipt_addresses:''
            }
        },
        props: {
            showAddScore: {
                type: Boolean,
                default: false
            },
            id: {
                type: Number
            },
            MerchantsList: {type: Array},
            rowInfo: {
                type: Object
            }
        },
        created() {
            if (this.id) {
                this.getDetail()
            }
        },
        methods: {
            saveDetail() {
                useVcode(this,()=>{
                    let self = this
                    this.$refs.saveChn.validate(valid => {
                        if (valid) {
                            // self.MerchantsList.find(function (value) {
                            //     return value.id == self.AddScoreInfo.platform
                            // })
                            // debugger
                            let str = "<div style='text-align:left;margin-left:50px;margin-right:50px;'>"
                            str += "<p>备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:&nbsp;" + (!self.AddScoreInfo.remark ? '' : self.AddScoreInfo.remark) + "</p></div>"
                            this.$confirm(str, '追分确认', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning',
                                center: true,
                                dangerouslyUseHTMLString: true
                            }).then(() => {
                                sendRechargeOrderschase(this.id, this.AddScoreInfo).then(res => {
                                    if (res.status === 0) {
                                        this.$message({message: `追分成功，已追分${this.rowInfo.amount}元。`, type: 'success'})
                                        this.$emit('update:showAddScore', false)
                                    }
                                })

                            })

                        }
                    })
                })
            },
            getDetail() {
                getOrderInfoByID(this.id).then(res => {
                        if (res.status === 0) {
                            this.DetailInfo = res.data
                            let {amount, pay_type, receipt_ext, recharge_amount,real_amount} = res.data
                            this.DetailInfo = {
                                amount,
                                pay_type,
                                receipt_ext,
                                recharge_amount,
                                real_amount
                            }
                            this.payInfo = JSON.parse(this.DetailInfo.receipt_ext)
                            getReceiptAddresses(Object.assign(this.DetailInfo, {
                                page_no: 1,
                                page_size: 100,
                                pay_type: pay_type
                            })).then(res => {
                                if (res.status === 0) {
                                    res.data.items.forEach((item) => {
                                        if (item.id === this.rowInfo.receipt_address_id) {
                                            let lable = ''
                                            if(item.nickname == null){
                                                let receipt_name = item.first_name + item.last_name
                                                lable = item.id + '-' + FormarOrderMethodType(item.receipt_type) + "-" + (receipt_name != 0 ? receipt_name +"-"+ item.account : item.mch_name)
                                            }else{
                                                lable = item.id + '-' + FormarOrderMethodType(item.receipt_type) + "-" + item.nickname +"-"+ item.account
                                            }
                                            this.receipt_addresses = lable
                                        }
                                    })
                                }
                            })}})}

        },
        watch: {
            showAddScore() {
                this.showDialog = this.showAddScore
            }
        },
        filters: {
            FormartToText(value) {
                return FormartPayType(value)
            }
        }
    }
</script>
